var wikilinkno;
var xmlHttp;
var currentPos = null;
var dataDiv;
var dataTable;
var dataTableBody;
var curX=0,curY=0;
var gDivMoveFlag = false;
var gDisplayFlag = false;
var gUpdateFlag = true;
var gDoctitle = null;
var gCurWidth=0,gCurHeight=0;
window.onload=initVars;
var IE = document.all?true:false;
String.prototype.Trim = function(){ return this.replace(/^\s*|\s*$/g,"");}

function getCursor(){
 var e;
 if(IE){
   e=window.event;
 }else{
   e=arguments[0];
 }
 if(e.pageX||e.pageY){
   curX=e.pageX;
   curY=e.pageY;
 }else{
   if(e.clientX||e.clientY){
     if (document.body.scrollTop > document.documentElement.scrollTop) {
       curY = e.clientY + document.body.scrollTop;
     } else {
       curY = e.clientY + document.documentElement.scrollTop;
     }
   
     if (document.body.scrollLeft > document.documentElement.scrollLeft) {
    curX = e.clientX + document.body.scrollLeft+10;
  } else {
    curX = e.clientX + document.documentElement.scrollLeft+10;
	  }
    }
  }
}

function createXMLHttpRequest() {
  if (window.ActiveXObject) {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else if (window.XMLHttpRequest) {
      xmlHttp = new XMLHttpRequest();                
  }
  if (!xmlHttp) {
	window.alert(JS_LANG['notcreatobj']);
	return false;
  }
}

function send_request(url) {
  createXMLHttpRequest();
  xmlHttp.onreadystatechange = processRequest;
  xmlHttp.open("GET", url, true);
  xmlHttp.send(null);
}

function handle_mouseover_IE()
{
  if (!gUpdateFlag)  return;
  showDocSummary_IE(this);
  gUpdateFlag = false;
}

 
function handle_mouseover()
{
  if (!gUpdateFlag) return;
  showDocSummary();
  gUpdateFlag = false;
}

function processlinks() {
      var links=document.links;
      for(i=0;i<links.length;i++){
      	  var vhref=links[i].href;
         if(vhref!=null){
         	if(IE){cls=links[i].className; }else{ cls=links[i].getAttribute("class");}
         	if(cls=='innerlink') links[i].setAttribute("target","_blank");
         	if ((vhref.indexOf("doc.php?action=view")!=-1)&&(cls=='innerlink'))
			{
            	wikilinkno="wikilink"+i;  
	            if(IE==true){
	            	links[i].onmouseover=handle_mouseover_IE;
	               	links[i].onmouseout=function (){gUpdateFlag = true;}
	               	links[i].setAttribute("id",wikilinkno);
	               	links[i].href="doc.php?action=view&title="+links[i].innerHTML;
	            }else{
	               	links[i].setAttribute("id",wikilinkno);
	               	links[i].onmouseover = handle_mouseover;
	               	links[i].onmouseout=function (){gUpdateFlag = true;}
	            }
	        }
        }
     }
  }


function initVars() {
  if (document)
    dataTableBody = document.getElementById("summaryDataBody");           
    dataTable = document.getElementById("summaryData");
    dataDiv = document.getElementById("popup");
    dataDiv.onmouseleave = onMouseLeave;  
    dataDiv.onmousedown = onMouseDown;
    dataDiv.onmouseup = onMouseUp;
    document.onmousemove = onMouseMove;
    processlinks();
}


function onMouseLeave(){
    if (gDisplayFlag || gDivMoveFlag) return ; 
    clearData();
}


function onMouseDown(){
  gDivMoveFlag = true;
  if(IE==true)
    getCursor(event);
  else {  
    try {
     window.constructor.prototype.__defineGetter__("event", window_prototype_get_event);
    }catch(e) {}
  
    curX = event.pageX;
    curY = event.pageY; 
  }
  gCurWidth = curX - parseInt(dataDiv.style.left);
  gCurHeight = curY - parseInt(dataDiv.style.top);
  dataDiv.style.cursor="hand";
  document.onselectstart=new Function("event.returnValue=false");
}

function onMouseUp(){
  gDivMoveFlag = false;
  dataDiv.style.cursor="default";
  document.onselectstart=new Function("event.returnValue=true");
} 


function onMouseMove(){       
  if (!gDivMoveFlag) return ; 
  if(IE==true)  getCursor(event);
  else {
    try {
     window.constructor.prototype.__defineGetter__("event", window_prototype_get_event);
    }catch(e) {}
   curX = event.pageX;
   curY = event.pageY; 
  }
  dataDiv.style.left = (curX-gCurWidth) + "px";
  dataDiv.style.top = (curY-gCurHeight) + "px";
}

function window_prototype_get_event()
{
 var func = arguments.callee.caller; 
 var evt; 
 while(func != null){
  evt = func.arguments[0]; 
  if(evt && (evt.constructor == Event || evt.constructor == MouseEvent)) return evt; 
  func = func.caller; 
 }
 return null; 
}

function showDocSummary(){
	try {
	window.constructor.prototype.__defineGetter__("event", window_prototype_get_event);
	}catch(e) {}
	curX = event.pageX;
	curY = event.pageY;
	var tempObj = event.target;
	var docTitle=tempObj.firstChild.data;
	if (0 == docTitle.Trim().length)
		return;
	while(tempObj.nodeName.toUpperCase() != "A"){
		tempObj = tempObj.parentNode;
	}
	createXMLHttpRequest();
	offsetEl = tempObj;
	var vhref=tempObj.href;
	if (vhref.substring(vhref.lastIndexOf("=")+1,vhref.length) != docTitle){
		tempObj.href = vhref.substring(0,vhref.lastIndexOf("=")+1) + docTitle; 
	}
	str = tempObj.href.substring(vhref.indexOf("title=")+6,vhref.length);
	url = "doc.php?action=insummary&title="+str;
 
	if (docTitle == gDoctitle && dataDiv.style.visibility == "visible")	return;
	gDoctitle = docTitle;
	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange = callback;
	xmlHttp.send(null);
}
               
function showDocSummary_IE(element) {
	getCursor(event);           
    createXMLHttpRequest();
    offsetEl = element;
    var FLAG = "doc.php?action=view&title=";
	var vhref = element.href;
	var beginPos = vhref.indexOf(FLAG);
	if (beginPos < 0)	return;
	var docTitle=element.innerText.Trim();
	if (docTitle == gDoctitle && dataDiv.style.visibility == "visible")	return;
	gDoctitle =docTitle;  
    url = "doc.php?action=insummary&title="+gDoctitle;
    xmlHttp.open("get",url,true);
    //xmlHttp.setRequestHeader("Content-Type","text/html;charset=utf-8");
    xmlHttp.onreadystatechange = callback;
    xmlHttp.send(null);
}
        
function callback() {
   if (xmlHttp.readyState == 4) {
     if (xmlHttp.status == 200) {
         setData(xmlHttp.responseXML);
     }
   }
}
        

function setData(courseData) {            
    var ind = dataTableBody.childNodes.length;            
    for (var i = ind - 1; i >= 0 ; i--) {
        dataTableBody.removeChild(dataTableBody.childNodes[i]);       
    }
    var flag = xmlHttp.responseXML.getElementsByTagName("flag")[0].firstChild.data;
    var docid = xmlHttp.responseXML.getElementsByTagName("docid")[0].firstChild.data;
    var doctitle = xmlHttp.responseXML.getElementsByTagName("doctitle")[0].firstChild.data;
    var summary=xmlHttp.responseXML.getElementsByTagName("summary")[0].firstChild.data;
    var firstimg=xmlHttp.responseXML.getElementsByTagName("firstimg")[0].firstChild.data;
    closeData = constructHead();
    dataTableBody.appendChild(createRow(closeData));
    if(flag=="0"){
      dataTable.setAttribute("width", "200");
      setOffsets();
      linkdata="doc.php?action=view&title="+gDoctitle;
      doctitleData="<tr><td width=\"300\" bgcolor=\"#F9F7F7\" align=\"center\"><br><br><a target='_blank' href=\""+linkdata+"\"><font size='2' color='red' >"+JS_LANG['creatdoc']+"</font></a><br><br><br></td></tr>";
      dataTableBody.appendChild(createRow(constructContent(doctitleData, "")));
    }else{
      dataTable.setAttribute("width", "300");
      setOffsets();
      if(firstimg.Trim().length!=0){
        firstimgData = constructIamge(firstimg);
      }else{
      	firstimgData="";
      }
      summary = constructSummary(summary); 			
 	  if(flag=="2"){
 	  //	summary='<tr><td colspan="2">'+JS_LANG['summary']+'<a href="http://www.hoodong.com">'+JS_LANG['HDWiki']+'</a> </td></tr>'+summary;
 	  }
 	  dataTableBody.appendChild(createRow(constructContent(firstimgData, summary)));
    }
    dataDiv.style.visibility ="visible";
}
        

function constructHead() { 
  	linkdata1="doc.php?action=view&title="+gDoctitle;
 
	var imgscr = (gDisplayFlag == true)? "images/wiki/tudinged_normal.gif" :"images/wiki/tuding_normal.gif";
    if (IE)
    	closeData = "<td><table width=\"315\"  border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"><tr><td width=\"4\" bgcolor=\"#D1E7FC\"></td><td width=\"19\" bgcolor=\"#D1E7FC\"><div align=\"center\"><img src=\"images/wiki/momo_3.gif\" width=\"13\" height=\"13\" /></div></td><td width=\"252\" bgcolor=\"#D1E7FC\" class=\"momozth\"><a target='_blank' href=\""+linkdata1+"\"><font color=\"#ff0000\">"+ gDoctitle +"</font></a></td><td width=\"145\" bgcolor=\"#D1E7FC\">&nbsp;</td><td width=\"21\" bgcolor=\"#D1E7FC\"><img src=\""+ imgscr +"\" onmouseover=\"changImgholdStatus_IE(this)\" onmouseout=\"changImgholdStatus_IE(this)\" onclick=\"changImgholdType_IE(this);\" width=\"12\" height=\"10\" /></td><td width=\"23\" bgcolor=\"#D1E7FC\"><img src=\"images/wiki/guanbi_normal.gif\" onmouseover=\"changImgcloseStatus_IE(this)\" onmouseout=\"changImgcloseStatus_IE(this)\" onclick=\"clearData()\" width=\"14\" height=\"14\" /></td><td width=\"4\" bgcolor=\"#D1E7FC\"></td></tr></table></td>";
    else  
        closeData = "<td><table width=\"315\"  border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"><tr><td width=\"4\" bgcolor=\"#D1E7FC\"></td><td width=\"19\" bgcolor=\"#D1E7FC\"><div align=\"center\"><img src=\"images/wiki/momo_3.gif\" width=\"13\" height=\"13\" /></div></td><td width=\"152\" bgcolor=\"#D1E7FC\" class=\"momozth\"><a target='_blank' href=\""+linkdata1+"\"><font color=\"#ff0000\">"+ gDoctitle +"</font></a></td><td width=\"145\" bgcolor=\"#D1E7FC\">&nbsp;</td><td width=\"21\" bgcolor=\"#D1E7FC\"><img src=\""+ imgscr +"\" onmouseover=\"changImgholdStatus()\" onmouseout=\"changImgholdStatus()\" onclick=\"changImgholdType();\" width=\"12\" height=\"10\" /></td><td width=\"23\" bgcolor=\"#D1E7FC\"><img src=\"images/wiki/guanbi_normal.gif\" onmouseover=\"changImgcloseStatus()\" onmouseout=\"changImgcloseStatus()\" onclick=\"clearData()\" width=\"14\" height=\"14\" /></td><td width=\"4\" bgcolor=\"#D1E7FC\"></td></tr></table></td>";
    return closeData;
}
        
function constructIamge(v_imgscr) { 
  var firstimgData ="";
  if(v_imgscr != "null"){
  	firstimgData ="<tr><td colspan=\"2\" bgcolor=\"#F9F7F7\"  align=\"center\"><img src=\""+ v_imgscr +"\" vspace=\"10\" /></td></tr>";
  }
  return firstimgData
}
function constructSummary(summary) {
  if(summary == "null") 
  {
     summary = "<tr><td width=\"4\" bgcolor=\"#F9F7F7\"></td><td bgcolor=\"#F9F7F7\" class=\"momozt12\"><br>&nbsp;&nbsp;&nbsp;&nbsp;<b>"+JS_LANG['summary']+":</b>"+JS_LANG['none']+"</td></tr>";
  } else {  
     summary = "<tr><td width=\"4\"  bgcolor=\"#F9F7F7\"></td><td bgcolor=\"#F9F7F7\" class=\"momozt12\"><br>&nbsp;&nbsp;&nbsp;&nbsp;<b>"+JS_LANG['summary']+":</b>"+ summary +"...</td></tr>";
     summary =summary+ "<tr><td colspan=\"2\" bgcolor=\"#F9F7F7\" align=\"right\"><a target=\"_blank\"  href=\"doc.php?action=view&title="+gDoctitle+"\"><b>"+JS_LANG['showalldoc']+"</b></a></td></tr>";
  }
  return summary;    
} 

function constructContent(v_img, v_summary) {
	var head = "<tr><td colspan=\"8\" valign=\"top\" background=\"images/wiki/momobg.gif\"><table width=\"98%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"><tr><td height=\"3\"></td></tr><tr><td><table class=\"biankuangmomo\" width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
	var end = "<tr><td height=\"4\" colspan=\"2\" bgcolor=\"#F9F7F7\"></td></tr></table></td></tr><tr><td height=\"2\"></td></tr></table></td>";
	return head+v_img+v_summary+end;
}               
function createRow(v_data) {            
    var row, cell, txtNode;
    row = document.createElement("tr");
    cell = document.createElement("td");
    cell.innerHTML=v_data;
    row.appendChild(cell);
    return row;  
}

function setOffsets() {
     if (gDisplayFlag) return;
     if(curX+dataDiv.offsetWidth < document.documentElement.clientWidth){
         dataDiv.style.left = curX + "px";
     }else{
	 dataDiv.style.left = curX-dataDiv.offsetWidth -10+ "px";
     }
     dataDiv.style.top = curY +5+ "px";
}

function clearData() {         
    var ind = dataTableBody.childNodes.length;            
    for (var i = ind - 1; i >= 0 ; i--)dataTableBody.removeChild(dataTableBody.childNodes[i]);       
    dataDiv.style.visibility ="hidden";
    gDisplayFlag =false;
}
        
function changImgcloseStatus_IE(v_scr)
{
	var imgScr = v_scr.src; window.status=imgScr;
	var head = imgScr.substring(0, imgScr.lastIndexOf("/")+1);
	if (imgScr.substring(imgScr.lastIndexOf("/")+1, imgScr.length) == "guanbi_normal.gif")v_scr.src = head + "guanbi_over.gif";	else v_scr.src = head + "guanbi_normal.gif";
}

function changImgholdStatus_IE(v_scr)
{
	var imgScr = v_scr.src;
	var head = imgScr.substring(0, imgScr.lastIndexOf("_")+1);
	if (imgScr.substring(imgScr.lastIndexOf("_")+1, imgScr.length) == "normal.gif")v_scr.src = head + "over.gif"; else v_scr.src = head + "normal.gif";
}

function changImgholdType_IE(v_scr)
{
	gDisplayFlag = (gDisplayFlag == true)? false :true;
	var imgScr = v_scr.src;
	var head = imgScr.substring(0, imgScr.lastIndexOf("/")+1);
	var imgName = imgScr.substring(imgScr.lastIndexOf("/")+1, imgScr.length);
	if (imgName == "tudinged_over.gif")	v_scr.src = head + "tuding_over.gif"; else v_scr.src = head + "tudinged_over.gif";
}

function changImgcloseStatus()
{
	try {window.constructor.prototype.__defineGetter__("event", window_prototype_get_event);}catch(e) {} 
	var v_scr = event.target;
	var imgScr = v_scr.src;
	var head = imgScr.substring(0, imgScr.lastIndexOf("/")+1);
	if (imgScr.substring(imgScr.lastIndexOf("/")+1, imgScr.length) == "guanbi_normal.gif") v_scr.src = head + "guanbi_over.gif";	else v_scr.src = head + "guanbi_normal.gif";
}

function changImgholdStatus()
{
	try {window.constructor.prototype.__defineGetter__("event", window_prototype_get_event);}catch(e) {} 
	var v_scr = event.target;
	var imgScr = v_scr.src;
	var head = imgScr.substring(0, imgScr.lastIndexOf("_")+1);
	if (imgScr.substring(imgScr.lastIndexOf("_")+1, imgScr.length) == "normal.gif") v_scr.src = head + "over.gif";else v_scr.src = head + "normal.gif";
}

function changImgholdType()
{
	gDisplayFlag = (gDisplayFlag == true)? false :true;
	try { window.constructor.prototype.__defineGetter__("event", window_prototype_get_event);}catch(e) {} 
	var v_scr = event.target;
	var imgScr = v_scr.src;
	var head = imgScr.substring(0, imgScr.lastIndexOf("/")+1);
	var imgName = imgScr.substring(imgScr.lastIndexOf("/")+1, imgScr.length);
	if (imgName == "tudinged_over.gif")	v_scr.src = head + "tuding_over.gif";else v_scr.src = head + "tudinged_over.gif";
}
